﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace VersionComparer.UI
{
    public partial class ControlContainerForm<T> : Form
        where T: UserControl,IContainedControl,new()
    {
        T control;
        public ControlContainerForm()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
            control = new T ();
            Text = ((IContainedControl)control).Text;
            control.Dock = DockStyle.Fill;
            mainPanel.Controls.Add(control);
            KeyPreview = true;
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (!ShowCancel && !ShowOK && keyData == Keys.Escape)
            {
                OnCancel();
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
        public bool ShowCancel
        {
            get
            {
                return btCancel.Visible;
            }
            set
            {
                btCancel.Visible = value;
            }
        }

        public bool ShowOK
        {
            get
            {
                return btOK.Visible;
            }
            set
            {
                btOK.Visible = value;
            }
        }
        protected virtual void OnClose()
        {
            control.Close(true);
            DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (Owner != null)
                Icon = Owner.Icon;
        }
        protected virtual void OnCancel()
        {
            control.Close(false);
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
            Close();
        }

        private void btOK_Click(object sender, EventArgs e)
        {
            OnClose();
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            OnCancel();
        }

        public T Control
        {
            get
            {
                return control;
            }
        }
        protected override void OnPreviewKeyDown(PreviewKeyDownEventArgs e)
        {
            base.OnPreviewKeyDown(e);
        }
        private void ControlContainerForm_Load(object sender, EventArgs e)
        {
            if (!ShowCancel && !ShowOK)
                mainPanel.Dock = DockStyle.Fill;
        }
    }
}
